﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;

namespace Rio.Framework.Pages.CriteriaControls
{

    public sealed partial class TextIndexControl
    {

        /// <summary>
        /// Get or set the textbox text property
        /// </summary>
        public override string InputValue
        {
            get
            { 
                return textboxInput.Text;
            }
            set
            {
                if (value != null)
                {
                    textboxInput.Text = value;
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TextIndexControl"/> class.
        /// </summary>
        public TextIndexControl()
        {
            InitializeComponent();

            _originalBorderBrush = this.textboxInput.BorderBrush as LinearGradientBrush;
        }

        /// <summary>
        /// Create an instance of the control
        /// </summary>
        /// <param name="p_oindex"><see cref="Index"/> instance</param>
        /// <param name="P_szValue">Default value</param>
        /// <param name="P_SetDefaultValue">Use default rio value</param>
        /// <param name="P_isDocumentSearchIndex">if set to <c>true</c> [p_is document search index].</param>
        public TextIndexControl(Index p_oindex, string P_szValue = null, bool P_SetDefaultValue = false, bool P_isDocumentSearchIndex = false)
            : this()
        {
            CurrentIndex = p_oindex;
            LabelControl.Text = CurrentIndex.IndexLabel;

            if (P_szValue != null && !string.Empty.Equals(P_szValue))
            {
                InputValue = P_szValue;
            }
            else
            {
                if (P_SetDefaultValue)
                    this.SetDefaultValue();
            }

            //Mandatory indication
            LabelMandatory.Visibility = CurrentIndex.IsMandatory ? Visibility.Visible : Visibility.Collapsed;

            FilterButton.Visibility = Visibility.Visible;

            //Add case sensitive event
            if (CurrentIndex.IsCaseSensitive)
                this.textboxInput.TextChanged += textboxInput_TextChanged;

            //Adding Max length 
            this.SetMaxLength();


            IsDocumentSearchIndex = P_isDocumentSearchIndex;
        }

        /// <summary>
        /// Display filter window with a list of values input
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonInput_Click(object sender, RoutedEventArgs e)
        {
            OnFilterClick(new Events.FilterClickEventArgs(this.CurrentIndex));
        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            OnRemoveControlClick(new EventArgs());
        }

        /// <summary>
        /// Set the maxLength property of textbox
        /// </summary>
        public override void SetMaxLength()
        {
          try
          {
              this.textboxInput.MaxLength = Convert.ToInt16(CurrentIndex.ValueMaxLength.ToString());
          }
          catch (Exception)
          {
              this.textboxInput.MaxLength = Int16.MaxValue;
          }
        }

        /// <summary>
        /// Used to set border color of input control
        /// </summary>
        public override void ChangeBorderColor(Color color)
        {
            this.textboxInput.BorderBrush = new SolidColorBrush(color);
        }

        public override void RevertBorderColor()
        {
            this.textboxInput.BorderBrush = _originalBorderBrush;
        }

        /// <summary>
        /// Hide mandatory label
        /// </summary>
        public override void DisableMandatoryLabel()
        {
            this.LabelMandatory.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public override void DisableIndexLabel()
        {
            this.LabelControl.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Activate Removing feature
        /// </summary>
        public override void ActiveRemove()
        {
            this.DeleteButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Hides filter button
        /// </summary>
        public override void DisableFilter()
        {
            this.FilterButton.Visibility = Visibility.Collapsed;
        }


        #region Textbox events

        /// <summary>
        /// Throw validation event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textboxInput_LostFocus(object sender, RoutedEventArgs e)
        {
            OnValueValidated(EventArgs.Empty);
        }

        /// <summary>
        /// Used to set the textbox casing 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textboxInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            int selStart = textboxInput.SelectionStart;
            textboxInput.Text = textboxInput.Text.ToUpper();
            textboxInput.SelectionStart = selStart;
        }


        #endregion

    }
}
